package com.xiaomei.manager.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URLDecoder;
import java.util.Properties;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.log4j.Logger;

public class HttpSender {
	private static final String key = "9694358131015327";
	
	private static String url;// 应用地址
	private static String account;// 账号
	private static String pswd;// 密码
	private static boolean needstatus; //是否需要状态报告
	
	private static final Logger logger = Logger.getLogger(HttpSender.class);
	
	static {
		Properties prop = new Properties();
		try {
			InputStream is = HttpSender.class.getClassLoader().getResourceAsStream("msg.properties");
			prop.load(is);
			String msg_url = prop.getProperty("msg_url");
			if (msg_url != null) {
				url =  DESUtils.Decrypt(msg_url, DESUtils.hex2byte(key));
			}
			
			String msg_account = prop.getProperty("msg_account");
			if (msg_account != null) {
				account =  DESUtils.Decrypt(msg_account, DESUtils.hex2byte(key));
			}
			
			String msg_pswd = prop.getProperty("msg_pswd");
			if (msg_pswd != null) {
				pswd =  DESUtils.Decrypt(msg_pswd, DESUtils.hex2byte(key));
			}
			
			String msg_needstatus = prop.getProperty("msg_needstatus");
			if (msg_needstatus != null) {
				needstatus =  Boolean.valueOf(DESUtils.Decrypt(msg_needstatus, DESUtils.hex2byte(key)));
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("加载短信平台配置失败");
		} 
	}
	
	/**
	 * 批量发送短信
	 * @param mobile 手机号码，多个号码使用","分割
	 * @param msg 短信内容
	 * @return 返回值定义参见HTTP协议文档
	 * @throws Exception
	 */
	public static String batchSend(String mobile, String msg) throws Exception {
		HttpClient client = new HttpClient();
		GetMethod method = new GetMethod();
		try {
			URI base = new URI(url, false);
			method.setURI(new URI(base, "HttpBatchSendSM", false));
			method.setQueryString(new NameValuePair[] { 
					new NameValuePair("account", account),
					new NameValuePair("pswd", pswd), 
					new NameValuePair("mobile", mobile),
					new NameValuePair("needstatus", String.valueOf(needstatus)), 
					new NameValuePair("msg", msg),
					new NameValuePair("extno", null), 
				});
			int result = client.executeMethod(method);
			if (result == HttpStatus.SC_OK) {
				InputStream in = method.getResponseBodyAsStream();
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				byte[] buffer = new byte[1024];
				int len = 0;
				while ((len = in.read(buffer)) != -1) {
					baos.write(buffer, 0, len);
				}
				return URLDecoder.decode(baos.toString(), "UTF-8");
			} else {
				logger.error("mobile : " + mobile + ", HTTP ERROR Status: " + method.getStatusCode() + ":" + method.getStatusText());
				throw new Exception("HTTP ERROR Status: " + method.getStatusCode() + ":" + method.getStatusText());
			}
		} finally {
			method.releaseConnection();
		}
	}
}
